import hashlib
SECURITY_KEY = b"dyk212^$w1qf2dez0bw+e)%y1ulfdz@-3z!z(#r##bxb#(7z+v"

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad

import random
import string


def generate_password(length=10):
    # 定义可能的字符集：字母和数字
    characters = string.ascii_letters + string.digits

    # 从字符集中随机选择 length 个字符
    password = ''.join(random.choice(characters) for i in range(length))

    return password


class AESTool:
    @staticmethod
    def encrypt(plain_text):
        """
        加密
        :param plain_text:
        :param key:
        :return:
        """
        try:
            key = SECURITY_KEY[:16]
            cryptor = AES.new(key, AES.MODE_ECB)
            encrypted_text = cryptor.encrypt(pad(plain_text.encode('utf-8'), AES.block_size))
            return encrypted_text
        except Exception as e:
            return plain_text

    @staticmethod
    def decrypt(encrypted_text):
        """
        解密
        :param encrypted_text:
        :param key:
        :return:
        """
        try:
            key = SECURITY_KEY[:16]
            cryptor = AES.new(key, AES.MODE_ECB)
            plain_text = cryptor.decrypt(encrypted_text)
            plain_text = unpad(plain_text, AES.block_size).decode()
            return plain_text
        except Exception as e:
            return encrypted_text

if __name__ == '__main__':
    # pip install pycryptodome
    print(AESTool.encrypt("123456"))
    print(AESTool.decrypt(AESTool.encrypt("123456")))
